package com.sxfq.loansupermarket.backsystem.modular.theTest.controller;

import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.UserMapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.User;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TestQuestionsInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.UserForTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 进入考试入口
 *
 * @author py
 * @Date 2018年2月4日23:05:54
 */
@Controller
@RequestMapping("/recite")
public class ReciteController extends BaseController {
    @Autowired
    ITestModelService iTestModelService;

    @Autowired
    ITestingService iTestingService;

    @Autowired
    ITheTestService iTheTestService;

    @Autowired
    ICompletedTestService completedTestService;

    @Autowired
    IIncompleteTestService iIncompleteTestService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    private SqlMapper sqlMapper;

    @RequestMapping(value = "/getCtCount", method = RequestMethod.POST)
    @ResponseBody
    public int getCtCount(String titleType) {
        List<TestQuestionsInfo> getErrorList = iTestingService.getReciteList(titleType);
        if (getErrorList.size() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 修改当前页
     */
    @RequestMapping(value = "/updatesCurrentPage")
    @ResponseBody
    public Tip updatesCurrentPage() throws Exception {
        String userId = super.getPara("userId").trim();
        String chapter = super.getPara("chapter").trim();
        String currentPage = super.getPara("currentPage").trim();
        String sql = "";
        if (chapter.equals("口内")) {
            sql = "update user set  kounei_current_page =" + currentPage + " where id=" + userId + ";";
        } else if (chapter.equals("口外")) {
            sql = "update user set  kouwai_current_page =" + currentPage + " where id=" + userId + ";";
        } else if (chapter.equals("口修")) {
            sql = "update user set  kouxiu_current_page =" + currentPage + " where id=" + userId + ";";
        } else if (chapter.equals("口解")) {
            sql = "update user set  koujie_current_page =" + currentPage + " where id=" + userId + ";";
        } else if (chapter.equals("咿呀文化")) {
            sql = "update user set  yywh_current_page =" + currentPage + " where id=" + userId + ";";
        } else if (chapter.equals("其他")) {
            sql = "update user set  qita_current_page =" + currentPage + " where id=" + userId + ";";
        }
        sqlMapper.update(sql);
        return SUCCESS_TIP;
    }

    @RequestMapping("/kounei")
    public String kounei(Model model) {
        String titleType = "口内";
        List<TestQuestionsInfo> getErrorList = iTestingService.getReciteList(titleType);
        model.addAttribute("testQuestionsInfoListl", getErrorList.size());
        model.addAttribute("testQuestionsInfoList", getErrorList);
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        UserForTestInfo userForTestInfo=  iTestingService.getReciteCurrentPage(id);
        model.addAttribute("currentPage", userForTestInfo.getKouneiCurrentPage());
        model.addAttribute("userName", user.getName());
        model.addAttribute("userId", ShiroKit.getUser().getId());
        model.addAttribute("chapter", "口内");
        return "/theTest/testModel/beiti.html";
    }

    @RequestMapping("/kouwai")
    public String kouwai(Model model) {
        String titleType = "口外";
        List<TestQuestionsInfo> getErrorList = iTestingService.getReciteList(titleType);
        model.addAttribute("testQuestionsInfoListl", getErrorList.size());
        model.addAttribute("testQuestionsInfoList", getErrorList);
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        UserForTestInfo userForTestInfo=  iTestingService.getReciteCurrentPage(id);
        model.addAttribute("currentPage", userForTestInfo.getKouwaiCurrentPage());
        model.addAttribute("userName", user.getName());
        model.addAttribute("userId", ShiroKit.getUser().getId());
        model.addAttribute("chapter", "口外");
        return "/theTest/testModel/beiti.html";
    }

    @RequestMapping("/kouxiu")
    public String kouxiu(Model model) {
        String titleType = "口修";
        List<TestQuestionsInfo> getErrorList = iTestingService.getReciteList(titleType);
        model.addAttribute("testQuestionsInfoListl", getErrorList.size());
        model.addAttribute("testQuestionsInfoList", getErrorList);
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        UserForTestInfo userForTestInfo=  iTestingService.getReciteCurrentPage(id);
        model.addAttribute("currentPage", userForTestInfo.getKouxiuCurrentPage());
        model.addAttribute("userName", user.getName());
        model.addAttribute("userId", ShiroKit.getUser().getId());
        model.addAttribute("chapter", "口修");
        return "/theTest/testModel/beiti.html";
    }

    @RequestMapping("/koujie")
    public String koujie(Model model) {
        String titleType = "口解";
        List<TestQuestionsInfo> getErrorList = iTestingService.getReciteList(titleType);
        model.addAttribute("testQuestionsInfoListl", getErrorList.size());
        model.addAttribute("testQuestionsInfoList", getErrorList);
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        UserForTestInfo userForTestInfo=  iTestingService.getReciteCurrentPage(id);
        model.addAttribute("currentPage", userForTestInfo.getKoujieCurrentPage());
        model.addAttribute("userName", user.getName());
        model.addAttribute("userId", ShiroKit.getUser().getId());
        model.addAttribute("chapter", "口解");
        return "/theTest/testModel/beiti.html";
    }

    @RequestMapping("/yywh")
    public String yywh(Model model) {
        String titleType = "咿呀文化";
        List<TestQuestionsInfo> getErrorList = iTestingService.getReciteList(titleType);
        model.addAttribute("testQuestionsInfoListl", getErrorList.size());
        model.addAttribute("testQuestionsInfoList", getErrorList);
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        UserForTestInfo userForTestInfo=  iTestingService.getReciteCurrentPage(id);
        model.addAttribute("currentPage", userForTestInfo.getYywhCurrentPage());
        model.addAttribute("userName", user.getName());
        model.addAttribute("userId", ShiroKit.getUser().getId());
        model.addAttribute("chapter", "咿呀文化");
        return "/theTest/testModel/beiti.html";
    }

    @RequestMapping("/qita")
    public String qita(Model model) {
        String titleType = "其他";
        List<TestQuestionsInfo> getErrorList = iTestingService.getReciteList(titleType);
        model.addAttribute("testQuestionsInfoListl", getErrorList.size());
        model.addAttribute("testQuestionsInfoList", getErrorList);
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        UserForTestInfo userForTestInfo=  iTestingService.getReciteCurrentPage(id);
        model.addAttribute("currentPage", userForTestInfo.getQitaCurrentPage());
        model.addAttribute("userName", user.getName());
        model.addAttribute("userId", ShiroKit.getUser().getId());
        model.addAttribute("chapter", "其他");
        return "/theTest/testModel/beiti.html";
    }


    private String getDailyInterestRate(String dailyInterestRate) {
        if (dailyInterestRate.indexOf(".") > 0) {
            //正则表达
            dailyInterestRate = dailyInterestRate.replaceAll("0+?$", "");//去掉后面无用的零
            dailyInterestRate = dailyInterestRate.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        return dailyInterestRate;
    }

    public String formatTime(Long totleSecond) {
        Long hour = totleSecond / 3600;
        Long minute = (totleSecond % 3600) / 60;
        Long second = totleSecond % 60;
        String hours = formatTimeNumber(hour);
        String minutes = formatTimeNumber(minute);
        String seconds = formatTimeNumber(second);
        return hours + ":" + minutes + ":" + seconds;
    }

    public String formatTimeNumber(Long number) {
        if (number < 10) {
            return "0" + number;
        } else {
            return number + "";
        }
    }
}
